package com.innovations.inn.controller;

import com.innovations.common.annotation.Log;
import com.innovations.common.core.controller.BaseController;
import com.innovations.common.core.domain.AjaxResult;
import com.innovations.common.core.page.TableDataInfo;
import com.innovations.common.enums.BusinessType;
import com.innovations.common.utils.poi.ExcelUtil;
import com.innovations.framework.web.service.TokenService;
import com.innovations.inn.domain.ColumnClassify;
import com.innovations.inn.service.IColumnClassifyService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 栏目分类Controller
 *
 * @author dp
 * @date 2024-08-10
 */
@Api(tags = "栏目分类")
@RestController
@RequestMapping("/inn/column/classify")
public class ColumnClassifyController extends BaseController {
    @Autowired
    private IColumnClassifyService columnClassifyService;
    @Autowired
    TokenService tokenService;

    /**
     * 查询栏目分类列表
     */
    @ApiOperation("查询栏目分类列表")
    @PreAuthorize("@ss.hasPermi('inn:classify:list')")
    @GetMapping("/list")
    public TableDataInfo list(ColumnClassify columnClassify) {
        startPage();
        List<ColumnClassify> list = columnClassifyService.selectColumnClassifyList(columnClassify);
        return getDataTable(list);
    }

    /**
     * 导出栏目分类列表
     */
    @PreAuthorize("@ss.hasPermi('inn:classify:export')")
    @Log(title = "栏目分类", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ColumnClassify columnClassify) {
        List<ColumnClassify> list = columnClassifyService.selectColumnClassifyList(columnClassify);
        ExcelUtil<ColumnClassify> util = new ExcelUtil<ColumnClassify>(ColumnClassify.class);
        util.exportExcel(response, list, "栏目分类数据");
    }

    /**
     * 获取栏目分类详细信息
     */
    @ApiOperation("获取栏目分类详细信息")
    @PreAuthorize("@ss.hasPermi('inn:classify:query')")
    @GetMapping(value = "/{classifyId}")
    public AjaxResult getInfo(@PathVariable("classifyId") Long classifyId) {
        return success(columnClassifyService.selectColumnClassifyByClassifyId(classifyId));
    }

    /**
     * 新增栏目分类
     */
    @ApiOperation("新增栏目分类")
    @PreAuthorize("@ss.hasPermi('inn:classify:add')")
    @Log(title = "栏目分类", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(HttpServletRequest request, @RequestBody ColumnClassify columnClassify) {
        Long userId = tokenService.getLoginUser(request).getUser().getUserId();
        columnClassifyService.completeData(columnClassify, userId, 1);
        return toAjax(columnClassifyService.insertColumnClassify(columnClassify));
    }

    /**
     * 修改栏目分类
     */
    @ApiOperation("修改栏目分类")
    @PreAuthorize("@ss.hasPermi('inn:classify:edit')")
    @Log(title = "栏目分类", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(HttpServletRequest request, @RequestBody ColumnClassify columnClassify) {
        Long userId = tokenService.getLoginUser(request).getUser().getUserId();
        columnClassifyService.completeData(columnClassify, userId, 2);
        return toAjax(columnClassifyService.updateColumnClassify(columnClassify));
    }

    /**
     * 删除栏目分类
     */
    @ApiOperation("删除栏目分类")
    @PreAuthorize("@ss.hasPermi('inn:classify:remove')")
    @Log(title = "栏目分类", businessType = BusinessType.DELETE)
    @DeleteMapping("/{classifyIds}")
    public AjaxResult remove(@PathVariable Long[] classifyIds) {
        return toAjax(columnClassifyService.deleteColumnClassifyByClassifyIds(classifyIds));
    }
}
